﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using DLM.Services.BonusRule;
using System.Configuration;
using DLM.Infrastructure.Configuration;
using DLM.Domain.Model;
using System.Web;
using DLM.Infrastructure.Logging;

namespace DLM.Services.BonusRule
{
    public class XmlRuleService : IBonusRuleService
    {
        private static string serverPath;

        static XmlRuleService()
        {
            serverPath = HttpRuntime.AppDomainAppPath; // System.Web.HttpContext.Current.Server.MapPath("~/");
            LoggingFactory.GetLogger().Log("XML path is " + serverPath);
            LoggingFactory.GetLogger().Log("XML1 path is " + HttpRuntime.AppDomainAppPath);
            //LoggingFactory.GetLogger().Log("XML2 path is " + HttpRuntime.AppDomainAppVirtualPath);
        }

        public Domain.Model.BonusRule GetRules()
        {
            //string serverPath = System.Web.HttpContext.Current.Server.MapPath("~/");
            if (!string.IsNullOrEmpty(serverPath))
            {
                string path = serverPath + ApplicationSettingsFactory.GetApplicationSettings().XmlRulePath;
                if (!string.IsNullOrEmpty(path))
                {
                    var el = XElement.Load(path);
                    DLM.Domain.Model.BonusRule rule = new Domain.Model.BonusRule();
                    rule.TemporaryDistrain = decimal.Parse(el.Element("temporarydistrain").Value);
                    rule.Salutation = decimal.Parse(el.Element("salutation").Value);
                    rule.Phone = decimal.Parse(el.Element("phone").Value);
                    rule.Distrain = new DistrainRule
                    {
                        Engaged = decimal.Parse(el.Element("distrain").Element("engaged").Value),
                        publish = decimal.Parse(el.Element("distrain").Element("publish").Value),
                        // Site = decimal.Parse(el.Element("distrain").Element("site").Value)
                    };

                    rule.Quit = new QuitRule
                    {
                        Frozen = decimal.Parse(el.Element("quit").Element("frozen").Value),
                        BreakAnAppointment = decimal.Parse(el.Element("quit").Element("breakanappointment").Value)
                    };

                    //rule.Publish = new PublishRule 
                    //{
                    //    Engaged = decimal.Parse(el.Element("publish").Element("engaged").Value)
                    //};

                    rule.Friend = new FriendRule
                    {
                        Invite = decimal.Parse(el.Element("friend").Element("invite").Value)
                    };

                    rule.Party = new PartyRule
                    {
                        Success = decimal.Parse(el.Element("party").Element("success").Value)
                    };

                    return rule;
                }
            }
          
            return null;
        }

        public void SetRules(DLM.Domain.Model.BonusRule rule)
        {
            if (rule == null)
            {
                throw new ArgumentNullException("Bonus rule");
            }
            string serverPath = System.Web.HttpContext.Current.Server.MapPath("~/");
            string path = serverPath + ApplicationSettingsFactory.GetApplicationSettings().XmlRulePath;
            var el = XElement.Load(path);
            el.Element("temporarydistrain").SetValue(rule.TemporaryDistrain);
            el.Element("salutation").SetValue(rule.Salutation);

            // Distrain
            el.Element("distrain").Element("engaged").SetValue(rule.Distrain.Engaged);
            el.Element("distrain").Element("publish").SetValue(rule.Distrain.publish);

            // Quit
            el.Element("quit").Element("frozen").SetValue(rule.Quit.Frozen);
            el.Element("quit").Element("breakanappointment").SetValue(rule.Quit.BreakAnAppointment);
            
            // Publish
            //el.Element("publish").Element("engaged").SetValue(rule.Publish.Engaged);

            // Fiend
            el.Element("friend").Element("invite").SetValue(rule.Friend.Invite);

            el.Element("party").Element("success").SetValue(rule.Party.Success);

            el.Save(path);

        }
    }
}
